home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 83 / MacAddict_083_2003-07.iso / mac / Software / Development / VLC Source 0.5.3.dmg / src / extras / libc.c < prev   
C/C++ Source or Header  |  2003-04-07  |  7KB  |  226 lines

  1. /*****************************************************************************
  2.  * libc.c: Extra libc function for some systems.
  3.  *****************************************************************************
  4.  * Copyright (C) 2002 VideoLAN
  5.  * $Id: libc.c,v 1.7 2003/02/08 22:20:28 massiot Exp $
  6.  *
  7.  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
  8.  *          Samuel Hocevar <sam@zoy.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. #include <string.h>                                              /* strdup() */
  25. #include <stdlib.h>
  26.  
  27. #include <vlc/vlc.h>
  28.  
  29. /*****************************************************************************
  30.  * getenv: just in case, but it should never be called
  31.  *****************************************************************************/
  32. #if !defined( HAVE_GETENV )
  33. char *vlc_getenv( const char *name )
  34. {
  35.     return NULL;
  36. }
  37. #endif
  38.  
  39. /*****************************************************************************
  40.  * strdup: returns a malloc'd copy of a string
  41.  *****************************************************************************/
  42. #if !defined( HAVE_STRDUP )
  43. char *vlc_strdup( const char *string )
  44. {
  45.     return strndup( string, strlen( string ) );
  46. }
  47. #endif
  48.  
  49. /*****************************************************************************
  50.  * strndup: returns a malloc'd copy of at most n bytes of string
  51.  * Does anyone know whether or not it will be present in Jaguar?
  52.  *****************************************************************************/
  53. #if !defined( HAVE_STRNDUP )
  54. char *vlc_strndup( const char *string, size_t n )
  55. {
  56.     char *psz;
  57.     size_t len = strlen( string );
  58.  
  59.     len = __MIN( len, n );
  60.     psz = (char*)malloc( len + 1 );
  61.  
  62.     if( psz != NULL )
  63.     {
  64.         memcpy( (void*)psz, (const void*)string, len );
  65.         psz[ len ] = 0;
  66.     }
  67.  
  68.     return psz;
  69. }
  70. #endif
  71.  
  72. /*****************************************************************************
  73.  * strcasecmp: compare two strings ignoring case
  74.  *****************************************************************************/
  75. #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
  76. int vlc_strcasecmp( const char *s1, const char *s2 )
  77. {
  78.     int i_delta = 0;
  79.  
  80.     while( !i_delta && *s1 && *s2 )
  81.     {
  82.         i_delta = *s1 - *s2;
  83.  
  84.         if( *s1 >= 'A' && *s1 <= 'Z' )
  85.         {
  86.             i_delta -= 'A' - 'a';
  87.         }
  88.  
  89.         if( *s2 >= 'A' && *s2 <= 'Z' )
  90.         {
  91.             i_delta += 'A' - 'a';
  92.         }
  93.  
  94.         s1++; s2++;
  95.     }
  96.  
  97.     return i_delta;
  98. }
  99. #endif
  100.  
  101. /*****************************************************************************
  102.  * strncasecmp: compare n chars from two strings ignoring case
  103.  *****************************************************************************/
  104. #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
  105. int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
  106. {
  107.     int i_delta = 0;
  108.  
  109.     while( n-- && !i_delta && *s1 )
  110.     {
  111.         i_delta = *s1 - *s2;
  112.  
  113.         if( *s1 >= 'A' && *s1 <= 'Z' )
  114.         {
  115.             i_delta -= 'A' - 'a';
  116.         }
  117.  
  118.         if( *s2 >= 'A' && *s2 <= 'Z' )
  119.         {
  120.             i_delta += 'A' - 'a';
  121.         }
  122.  
  123.         s1++; s2++;
  124.     }
  125.  
  126.     return i_delta;
  127. }
  128. #endif
  129.  
  130. /*****************************************************************************
  131.  * atof: convert a string to a double.
  132.  *****************************************************************************/
  133. #if !defined( HAVE_ATOF )
  134. double vlc_atof( const char *nptr )
  135. {
  136.     double f_result;
  137.     wchar_t *psz_tmp;
  138.     int i_len = strlen( nptr ) + 1;
  139.  
  140.     psz_tmp = malloc( i_len * sizeof(wchar_t) );
  141.     MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
  142.     f_result = wcstod( psz_tmp, NULL );
  143.     free( psz_tmp );
  144.  
  145.     return f_result;
  146. }
  147. #endif
  148.  
  149. /*****************************************************************************
  150.  * lseek: reposition read/write file offset.
  151.  *****************************************************************************
  152.  * FIXME: this cast sucks!
  153.  *****************************************************************************/
  154. #if !defined( HAVE_LSEEK )
  155. off_t vlc_lseek( int fildes, off_t offset, int whence )
  156. {
  157.     return SetFilePointer( (HANDLE)fildes, (long)offset, NULL, whence );
  158. }
  159. #endif
  160.  
  161. /*****************************************************************************
  162.  * dgettext: gettext for plugins.
  163.  *****************************************************************************/
  164. char *vlc_dgettext( const char *package, const char *msgid )
  165. {
  166. #if defined( ENABLE_NLS ) \
  167.      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
  168.     return dgettext( package, msgid );
  169. #else
  170.     return (char *)msgid;
  171. #endif
  172. }
  173.  
  174. /*****************************************************************************
  175.  * wraptext: insert \n at convenient places. CAUTION: modifies its argument
  176.  *****************************************************************************/
  177. char *vlc_wraptext( char *psz_text, size_t i_line )
  178. {
  179.     size_t i_len = strlen(psz_text);
  180.     char * psz_line = psz_text;
  181.  
  182.     while ( i_len > i_line )
  183.     {
  184.         /* Look if there is a newline somewhere. */
  185.         char * psz_parser = psz_line;
  186.         while ( psz_parser <= psz_line + i_line && *psz_parser != '\n' )
  187.         {
  188.             psz_parser++;
  189.         }
  190.         if ( *psz_parser == '\n' )
  191.         {
  192.             i_len -= psz_parser + 1 - psz_line;
  193.             psz_line = psz_parser + 1;
  194.             continue;
  195.         }
  196.  
  197.         /* Find the furthest space. */
  198.         psz_parser = psz_line + i_line;
  199.         while ( psz_parser > psz_line && *psz_parser != ' ' )
  200.         {
  201.             psz_parser--;
  202.         }
  203.         if ( *psz_parser == ' ' )
  204.         {
  205.             *psz_parser = '\n';
  206.             i_len -= psz_parser + 1 - psz_line;
  207.             psz_line = psz_parser + 1;
  208.             continue;
  209.         }
  210.  
  211.         /* Wrapping has failed. Find the first space or newline after i_line. */
  212.         psz_parser = psz_line + i_line + 1;
  213.         while ( psz_parser < psz_line + i_len
  214.                  && *psz_parser != ' ' && *psz_parser != '\n' )
  215.         {
  216.             psz_parser++;
  217.         }
  218.  
  219.         if ( psz_parser < psz_line + i_len ) *psz_parser = '\n';
  220.         i_len -= psz_parser + 1 - psz_line;
  221.         psz_line = psz_parser + 1;
  222.     }
  223.  
  224.     return psz_text;
  225. }
  226.